package com.tbzz.mycloud;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.tbzz.mycloud.service.OrderHystrixService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
/**
 * 全局服务方法降级处理，具体的方法上存在1对1的服务方法降级时该方法默认采用1对1的配置，
 * 其余需要服务降级的方法需结合@HystrixCommand注解，采用全局处理服务降级的配置（就近原则）
 */

//@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {
    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = orderHystrixService.paymentInfo_OK(id) ;
        return result;
    }
    /**
     * 服务降级处理
     * @param id
     * @return
     */
    //1对1的服务方法降级
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            //设置服务调用峰值时间
//            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
//    })
//    @HystrixCommand//结合全局服务方法降级
    @GetMapping(value = "/consumer/payment/hystrix/timeOut/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return orderHystrixService.paymentInfo_TimeOut(id);
    }
    public String paymentTimeOutFallbackMethod(Integer id){
        return "线程池: "+Thread.currentThread().getName()+"paymentTimeOutFallbackMethod,id"+id+"000000";
    }
    // 下面是全局fallback方法
    public String payment_Global_FallbackMethod()
    {
        return "Global异常处理信息，请稍后再试，/(ㄒoㄒ)/~~";
    }
}
